// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import iView from 'iview'
import 'babel-polyfill'
import 'iview/dist/styles/iview.css'
import './assets/base.css'
import axios from 'axios'
import myBreadcrumb from './components/common/myBreadcrumb'
import MenuUtils from './utils/MenuUtils'
import routerUtil from './utils/routerUtil'

Vue.config.productionTip = false
Vue.use(iView)
Vue.component('myBreadcrumb', myBreadcrumb)
Vue.prototype.$ajax = axios
axios.defaults.withCredentials = true
axios.defaults.timeout = 8000
axios.defaults.headers['Content-Type'] = 'application/json'
// https://www.gztang.cn //192.168.31.21:8080
axios.defaults.baseURL = 'https://www.gztang.cn'

Vue.prototype.staticUrl = 'https://hycos-1257620396.file.myqcloud.com'
iView.Message.config({
  duration: 3
})

let brandName = window.sessionStorage.getItem('brandName')
if (brandName) {
  document.title = brandName
}
let routers = window.sessionStorage.getItem('routers')
if (routers) {
  let temp = []
  MenuUtils(temp, JSON.parse(routers))
  router.addRoutes(temp)
} else {
  // console.log(router)
  axios.get('/admin/user/info')
    .then(res => {
      if (res.data.success) {
        let routerData = routerUtil(res.data.data)
        window.sessionStorage.setItem('routers', JSON.stringify(routerData))
        let arr = []
        MenuUtils(arr, routerData)
        router.addRoutes(arr)
      }
    })
}
axios.interceptors.request.use(config => {
  iView.LoadingBar.start()
  return config
}, err => {
  iView.LoadingBar.error()
  return Promise.reject(err)
})
axios.interceptors.response.use(response => {
  if (response.data.success) {
    iView.LoadingBar.finish()
  } else {
    if (response.data.errorCode === 10010) {
      window.location.href = 'https://www.gztang.cn/login.html'
    }
    iView.LoadingBar.error()
    response.data.errorMessage && iView.Message.error(response.data.errorMessage)
  }
  return response
}, err => {
  iView.LoadingBar.error()
  return Promise.reject(err)
})

// 过滤器
Vue.filter('format', (val) => {
  if (!val) {
    return ''
  }
  let time = new Date(val)
  let y = time.getFullYear()
  let m = time.getMonth() + 1
  let d = time.getDate()
  let h = time.getHours()
  let mm = time.getMinutes()
  let s = time.getSeconds()
  return y + '-' + m + '-' + d + ' ' + h + ':' + mm + ':' + s
})

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})
